<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\modules\v1\controllers\parent\ParentController;
use app\models\User;
use app\config\Status;
use Yii;
use yii\data\ActiveDataProvider;

class UserController extends ParentController
{
    /**
     * @return ActiveDataProvider
     */
    public function actionIndex(){
        $params = \Yii::$app->request->getQueryParams();
        $query = User::find()
            ->with(['role', 'department', 'branch', 'part']);

        if(isset($params['department_id']) && (int)$params['department_id'] !== 0)
        {
//            return (int)$params['department_id'];
            $query->andWhere(['department_id'=> (int)$params['department_id']]);
        }

        if(isset($params['role_id']) && (int)$params['role_id'] !== 0)
        {
            $query->andWhere(['role_id'=> (int)$params['role_id']]);
        }

        if(isset($params['branch_id']) && (int)$params['branch_id'] !== 0)
        {
            $query->andWhere(['branch_id'=> (int)$params['branch_id']]);
        }

        if(isset($params['username']))
            $query->andWhere(['like','username', $params['username']]);
        if(isset($params['name']))
            $query->andWhere(['like','username', $params['name']]);
        $pageSize = 10;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                    'username' => SORT_ASC,
                ]
            ],
        ]);
    }

    /**
     * 根据用户ID请求一个用户的信息
     * @return User|array|null
     */
    public function actionView(){
        $a = \Yii::$app->controller->id;
        $id = \Yii::$app->request->get('id');
        $res = User::find()->where(['id' => $id])->with('department')->one();
        // 返回处理结果
        return $res;
    }

    /**
     * @return User|array
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate()
    {
        // 解析用户请求数据
        $reqParams = \Yii::$app->request->getBodyParams();
        // 调用模型进行处理
        $user = new User();
        $user->load($reqParams,'');
        // 返回处理结果
        if($user->create())
            return ['bCode'=>Status::SUCCESS,'bData'=>$user];
        return ['bCode'=>Status::FAIL,'bData'=>$user->errors];
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate() {
        $data = \Yii::$app->request->getBodyParams();
        $model = User::findOne($data['id']);
        $model->updated_at = time();
        $model->load($data,'');
        if ($model->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $model->errors
        ];
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdatePassword(){
        $params = \Yii::$app->request->getBodyParams();
        $id = \Yii::$app->user->id;
        $user = User::findOne($id);
        $oldPassword = $params['oldPassword'];
        $password = $params['password'];
        $password2 = $params['password2'];
        if(sha1($oldPassword) !== $user->password){
            return [
                'bCode' => Status::FAIL,
                'bData' => ['msg'=>'原密码错误']
            ];
        }
        if($password !== $password2){
            return [
                'bCode' => Status::FAIL,
                'bData' => ['msg'=>'确认密码不一致']
            ];
        }
        $user->password = sha1($password);
        $user->updated_at = time();
        if($user->save())
            return [
                'bCode' => Status::SUCCESS,
                'bData' => ['msg'=>'密码修改成功']
            ];
        return [
            'bCode' => Status::FAIL,
            'bData' => ['msg'=>'密码修改失败，save执行错误']
        ];

    }

    /**
     * @return User|null|\yii\web\IdentityInterface
     * @throws \Throwable
     */
    public function actionLogout(){
//        $user = User::findOne(\Yii::$app->user->id);
        $user = \Yii::$app->user->identity;
        /** @noinspection PhpUndefinedMethodInspection */
        return $user->updateToken();
    }
}